#!/bin/bash

# 设置错误时退出
set -e

# 设置版本号
VERSION="1.0.0"

echo "Starting build process..."

# 检查 Go 环境
if ! command -v go &> /dev/null; then
    echo "Error: Go is not installed"
    exit 1
fi

# 显示 Go 版本
echo "Using Go version:"
go version

# 创建输出目录
echo "Creating dist directory..."
mkdir -p dist

# 设置编译参数
LDFLAGS="-X main.Version=$VERSION"

# 编译 Linux 版本
echo "Building for Linux..."
GOOS=linux GOARCH=amd64 go build -ldflags "$LDFLAGS" -o dist/cross-pay-go_linux_amd64 ./src/main.go
if [ $? -eq 0 ]; then
    echo "Linux build successful"
else
    echo "Linux build failed"
    exit 1
fi

# 编译 Windows 版本
echo "Building for Windows..."
GOOS=windows GOARCH=amd64 go build -ldflags "$LDFLAGS" -o dist/cross-pay-go_windows_amd64.exe ./src/main.go
if [ $? -eq 0 ]; then
    echo "Windows build successful"
else
    echo "Windows build failed"
    exit 1
fi

# 编译 MacOS 版本
echo "Building for MacOS..."
GOOS=darwin GOARCH=amd64 go build -ldflags "$LDFLAGS" -o dist/cross-pay-go_darwin_amd64 ./src/main.go
if [ $? -eq 0 ]; then
    echo "MacOS build successful"
else
    echo "MacOS build failed"
    exit 1
fi

# 设置执行权限
echo "Setting executable permissions..."
chmod +x dist/cross-pay-go_linux_amd64
chmod +x dist/cross-pay-go_darwin_amd64

# 复制配置文件
echo "Copying configuration files..."
mkdir -p dist/config
cp config/config.yaml dist/config/
cp config/config.prod.yaml dist/config/

# 创建必要的目录
echo "Creating necessary directories..."
mkdir -p dist/logs

# 验证文件是否创建成功
echo "Verifying build outputs..."
ls -lh dist/
ls -lh dist/config/

echo "Build completed successfully. Binaries and configuration files are located in the dist directory." 